/*

Stickman Adventure - a game created by Daco & his cousin

Copyright (C) 2011-2012  DacoTaco & Wouterke

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


*/
#include "Player.h"
#include "Global.h"

Player::Player(const char* spritesheet,int xpos , int ypos , const char* sound[],int soundlength ) : MovingObject(spritesheet,xpos , ypos ,144,48/*192,64*/ ,OBJECT_PLAYER,sound ,soundlength)
{
	OBJECT_WALK_ANIMATION_DELAY = 0.1f;
	controls[0] = SDLK_RIGHT;
	controls[1] = SDLK_LEFT;
	controls[2] = SDLK_UP;
	controls[3] = SDLK_DOWN;
}

void Player::handle_events(SDL_Event e)
{
	if( e.type == SDL_KEYDOWN )
	{
		if(e.key.keysym.sym == controls[0])
		{
			velocity+=width/SHEET_FRAMES;
		}
		else if(e.key.keysym.sym == controls[1])
		{
			velocity-=width/SHEET_FRAMES;
		}
		else if(e.key.keysym.sym == controls[2])
		{
			if(yv==DEFAULT_YV)
			{
				yv=JUMP_YV;
				jump();
			}
		}
		else if(e.key.keysym.sym == controls[3])
		{
			isdown=true;
		}
	}
	if( e.type == SDL_KEYUP)
	{
		if(e.key.keysym.sym == controls[0])
		{
			velocity-=width/SHEET_FRAMES;
		}
		else if(e.key.keysym.sym == controls[1])
		{
			velocity+=width/SHEET_FRAMES;
		}
		else if(e.key.keysym.sym == controls[2])
		{
			if(yv>=0)
				yv=-1;
		}
		else if(e.key.keysym.sym == controls[3])
		{
			isdown=false;
		}
	}
	return;
}
void Player::SetControls(short* ptr_controls)
{
	memcpy(controls,ptr_controls,sizeof(short)*4);
	printf("%d %d %d %d \n",controls[0],controls[1],controls[2],controls[3]);
	return;
}
char Player::Update(int collision, int y_ground, int x_wall)
{
	if(dead)
	{
		if(!Mix_Playing(-1))
		{
			dead = 0;
			y = 0;
			x = 592;
		}
		else
			return 1;
	}
	gprintf("collision : %d\ny_ground : & %d\nx_Wall: %d\nLx & Rx & botY : %d & %d & %d\n",collision,y_ground,x_wall,x,x+width,y+height);
	if(velocity<0)status=1;
	if(velocity>0)status=0;
	char ret = 1;
	ret = MovingObject::Update(collision,y_ground,x_wall);
	if(y+height > map_y)
	{
		//death();
		y = map_y-height;
	}
	if(x+width > map_x)
		x = map_x-width;
	if( y+height <= 0 )
		y = 0;
	if( x+width <= 0)
		x = 0;

	return ret;
}


void Player::jump()
{
	if(sounds[0] && dead == 0)
		Mix_PlayChannel( -1, sounds[0], 0 );
}
void Player::death()
{
	if(sounds.size() >= 1 && sounds[1] && dead == 0)
		Mix_PlayChannel( -1, sounds[1], 0 );
	dead = 1;
}
void Player::down()
{
}
bool Player::walk()
{
	return 1;
}


SDL_Rect* Player::getImageClip()
{
	if(status>1){ //jump and sit down
		clip.x=(status-2)*width;
		clip.y=2*height;
	}
	else { //stand still and moving
		clip.x=frame*width;	
		clip.y=status*height;

	}
	clip.w=width;
	clip.h=height;
	return &clip;

}
SDL_Rect* Player::GetColRect()
{
	ColRect.x = x;
	if(yv==DEFAULT_YV)
		ColRect.y = y;
	else
		ColRect.y = y-yv;
	ColRect.w = width;
	ColRect.h = height;
	return &ColRect;
}
//TODO : fix and make it less hacky
void Player::CollisionAction(Object* o, int collision)
{
	return;
}
void Player::draw(SDL_Surface* destination,Object* screen)
{
	if(dead)
		return;
	Object::draw(destination,screen);
}

